﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Tetris
{
    class ThisGameLogic
    {
        private InputHandler _inputHandler;
        private bool _gameover;
        private int _score=0;
        private int _speed = 250;

        public ThisGameLogic(InputHandler i)
        {
            _inputHandler = i;
        }
        public int Score
        {
            get { return _score; }
        }
        public void StartGame()
        {
            Factory a = new Factory();
            GameField fil = new GameField(20, 10);
            
            Renderer r = new Renderer();
            r.PrintField(fil);
            while (!_gameover)
            {
                _inputHandler.QueryKeyBoard();
                FullRowCleaner(fil, fil.IsPositionFree);
                Shape s = a.MakeShape(fil);
                r.PrintScore(Score, fil);
                while (!s.FallenShape)
                {
                    s.Fall(fil.IsPositionFree, IsGameOver, fil.AddShape);
                    r.PrintShapes(s);
                    Thread.Sleep(_speed);
                }
            }
            r.PrintGameOver(fil);
            Console.ReadKey();
        }
        public void OnLeft(Shape s, GameField f)
        {
            s.MoveLeft(f.IsPositionFree);
        }
        public void OnRight(Shape s, GameField f)
        {
            s.MoveRight(f.IsPositionFree);
        }
        public void OnDown(Shape s, GameField f)
        {
            s.MoveDown();
        }
        public void OnRotate(Shape s, GameField f)
        {
            s.MoveLeft(f.IsPositionFree);
        }
        public void SetSpeed(int _score)
        {
            if (_score > 5)
            {
                _speed = 220;
            }
            else if(_score > 5)
            {
                _speed = 190;
            }
            else if(_score > 5)
            {
                _speed = 160;
            }
            else if (_score > 5)
            {
                _speed = 130;
            }
            else if (_score > 5)
            {
                _speed = 110;
            }
            else
            {
                _speed = 90;
            }
            
        }
        public void IsGameOver(Shape s)
        {
            foreach(Point item in s.Coordinates)
            if (item.y == 0)
            {
                _gameover= true;
            }
            else
            {
                _gameover= false;
            }
        }
        public void FullRowCleaner(GameField f, Func<int, int, int> IsPosFree)
        {
            for (int i = 0; i < f.Heights; i++)
            {
                if (IsPosFree(0, i) == 1)
                {
                    bool Full = true;
                    for (int j = 0; j < f.Widths; j++)
                    {
                        if (IsPosFree(j, i) != 1)
                        {
                            Full = false;
                        }
                    }
                    if (Full != false)
                    {
                        f.RemoveRow(i);
                        _score++;
                        i--;
                        SetSpeed(_score);
                    }
                }
            }
        }
    }
}
